/**
 * 
 */
var xmlHttp = null;

function GetXmlHttpObject() {
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
	}
	catch (e) {
		//Internet Explorer
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	if (xmlHttp == null) {
		alert ("Browser does not support HTTP Request");
	}
	return xmlHttp;
}

function insertAfter(newEl, targetEl) {
	var parentEl = targetEl.parentNode;
	if (parentEl.lastChild == targetEl) {
		parentEl.appendChild(newEl);
	} else {
		parentEl.insertBefore(newEl,targetEl.nextSibling);
	}            
}

function submit_comment(commentsID, user_id, user_name) {
	xmlHttp = GetXmlHttpObject();
	var url = "./comments_ajax.php";
	var comment_content = document.getElementById("reply-area-"+commentsID).value;
	var parameters = "post_id=" + commentsID + "&user_id=" + user_id + "&user_name=" + user_name + "&content=" + comment_content + "&act=" + "write";
	url = document.location + "wp-content/themes/twentyten/comments_ajax.php";
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
			if (xmlHttp.status == 200) {
				comment_flag = xmlHttp.responseText;
				alert(comment_flag);
			}
		}
	}
	xmlHttp.open("POST",url, true);
	xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	xmlHttp.send(parameters);
}

function toggle_comments(commentsID) {
	var comments_position = document.getElementById("comments-post-" + commentsID);
	if (comments_position.style.display == "") {
		comments_position.style.display = "none";
	} else if (comments_position.style.display == "none") {
		comments_position.style.display = "";
	}
}

function show_comments(JSONObject, commentsID) {
	var i = 0;
	var comments = JSONObject;
	var comments_position = document.getElementById("comments-post-" + commentsID);
	comments_position.style.display = "";
	if (comments) {
		while (comments[i]) {
			var comment_display = document.createElement("div");
			comment_display.className = "comment_para";
			comment_display.innerHTML = "<span class='comment_author_span'>"+comments[i].comment_author+"</span>"+" ";
			comment_display.innerHTML += "<span class='comment_date_span'>"+comments[i].comment_date+"</span>"+"<br />";
			comment_display.innerHTML += "<span class='comment_content_span'>"+comments[i].comment_content+"</span>";
			comments_position.appendChild(comment_display);
			i++;
		}
	}
	user_id = document.getElementById("user_id").value;
	user_name = document.getElementById("user_name").value;
	user_email = document.getElementById("user_email").value;
	user_url = document.getElementById("user_url").value; 
	if (user_id != 0) {
		var reply_area = document.createElement("div");
		reply_area.innerHTML = "<textarea id='reply-area-"+commentsID+"' class='comment_area'></textarea>";
		reply_area.innerHTML = reply_area.innerHTML + "<input type='button' class='reply_button' value='评论' onclick='submit_comment(" + commentsID + ", " + user_id + ", \"" + user_name + "\")' />";
		comments_position.appendChild(reply_area);
	} else {
		var prompt_login = document.createElement("div");
		prompt_login.innerHTML = "<span class='prompt_login'>请<a href='wp-login.php'>登录</a>后评论。或者<a href='wp-login.php?action=register'>注册</a>";
		comments_position.appendChild(prompt_login);
	}


	document.getElementById("comments-link-"+commentsID).onclick = new Function("toggle_comments("+commentsID+")");
}

function send_request(commentsID) {
	xmlHttp = GetXmlHttpObject();
	var url = "./comments_ajax.php";
	var parameters = "id=" + commentsID + "&act=" + "read";
	url = document.location + "wp-content/themes/twentyten/comments_ajax.php";
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
			if (xmlHttp.status == 200) {
				JSONObject = xmlHttp.responseText.parseJSON();
				show_comments(JSONObject, commentsID);
			}
		}
	}
	xmlHttp.open("POST",url, true);
	xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	xmlHttp.send(parameters);
}
